package com.example.server.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.question.Question;
import com.example.domain.question.QuestionES;
import com.example.domain.question.dto.QuestionAddDTO;
import com.example.domain.question.dto.QuestionEditDTO;
import com.example.domain.question.dto.QuestionQueryDTO;
import com.example.domain.question.vo.QuestionDetailVO;
import com.example.domain.question.vo.QuestionVO;
import com.example.elasticsearch.QuestionRepository;
import com.example.manager.QuestionCacheManager;
import com.example.mapper.question.QuestionMapper;
import com.example.security.exception.ServiceImplException;
import com.example.server.question.IQuestionServer;
import com.github.pagehelper.PageHelper;
import oj.common.core.contants.Constants;
import oj.common.core.domain.PageQueryDTO;
import oj.common.core.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServerImpl implements IQuestionServer {
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private QuestionCacheManager questionCacheManager;
    @Autowired
    private QuestionMapper questionMapper;
    //这里不进行实现这个方法，然后在mapper层中进行实现
    public List<QuestionVO> selectList(QuestionQueryDTO questionQueryDTO) {
        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());
        if(StrUtil.isNotEmpty(questionQueryDTO.getExcludeIdStr())) {
            //进行字符串拼接
            String[] excludeIdsArr = questionQueryDTO.getExcludeIdStr().split(Constants.SPLIT_SEM);
            //转化为相应的stream流
            Set<Long> collect = Arrays.stream(excludeIdsArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(collect);
        }
        return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public int add(QuestionAddDTO questionAddDTO) {
        //在增加的时候，先进行查询题目是否一样，否则用户体验感不好
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if(CollectionUtil.isNotEmpty(questionList)){
            throw new ServiceImplException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        //进行拷贝数据
        BeanUtil.copyProperties(questionAddDTO,question);

        QuestionES questionEs = new QuestionES();
        BeanUtil.copyProperties(questionAddDTO,questionEs);
        questionRepository.save(questionEs);
        questionCacheManager.addCache(question.getQuestionId());
        return questionMapper.insert(question);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            throw new ServiceImplException(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO  questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question, questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int delete(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question==null) {
            //资源不存在
            throw new ServiceImplException(ResultCode.FAILED_NOT_EXISTS);
        }
        //这里已经做了c端的缓存
        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(question.getQuestionId());
        return questionMapper.deleteById(questionId);
    }

    @Override
    public int update(QuestionEditDTO questionEditDTO) {
        Question oldQuestion = questionMapper.selectById(questionEditDTO.getQuestionId());
        if(oldQuestion==null) {
            //资源不存在
            throw new ServiceImplException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDTO.getMainFuc());

        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(oldQuestion,questionES);
        questionRepository.save(questionES);

        return questionMapper.updateById(oldQuestion);
    }
}
